package com.jshan.blog.adapter;

import com.jshan.blog.utils.FileUploadUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Slf4j
public class LocalStorageAdapter implements StorageAdapter {

    @Value("${local.file.path}")
    private String path;

    @Value("${local.file.url}")
    private String prefix;


    @Override
    @SneakyThrows
    public String uploadFile(MultipartFile file, String type, String storageType) {
        // 对文件重命名
        String filePath = path + type + "/";
        FileUploadUtils.exitPath(filePath);
        String fileName = FileUploadUtils.generateFileName(filePath, file.getOriginalFilename(), storageType);
        file.transferTo(new File(filePath + fileName));
        return prefix + type + "/" + fileName;
    }


}
